package experience;

import week6.ExpressionTree;
import week6.ExpressionTreeOp;

import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;

public class Translation {
    private Stack<ExpressionTree> tree;
    private Stack stack;

    public static void main(String[] args) {
        Translation translation =new Translation();
        String a="8 / 4 - 9 + 11 * 2";
        translation.Translate(a);
        System.out.println("中缀表达式树是");
        System.out.println(translation.getTree());
        System.out.println("后序遍历是"+translation.PostOrder());

        System.out.println("结果是"+translation.getResult());


    }

    public Translation(){
            tree = new Stack<ExpressionTree>();
            stack = new Stack();
        }
        private ExpressionTree getOperand(Stack<ExpressionTree> treeExpression){
            ExpressionTree num;
            num = treeExpression.pop();
            return num;
        }
        public ExpressionTree Translate(String expression){
            ExpressionTree operand1,operand2;
            char operator;
            String tempToken;
            Scanner parser = new Scanner(expression);
            while(parser.hasNext()){
                tempToken = parser.next();
                operator=tempToken.charAt(0);
                if ((operator == '+') || (operator == '-') || (operator=='*') || (operator == '/')){
                    if (stack.empty())
                        stack.push(tempToken);//当储存符号的栈为空时，直接进栈
                    else{
                        String a =stack.peek()+"";//因为当ope.peek()='-'时，计算机认为ope.peek()=='-'为false，所以要转化为string 使用equals（）方法
                        if (((a.equals("+"))||(a.equals("-")))&&((operator=='*')||(operator=='/')))
                            stack.push(tempToken);//当得到的符号的优先级大于栈顶元素时，直接进栈
                        else {
                            String s = String.valueOf(stack.pop());
                            char temp = s.charAt(0);
                            operand1 = getOperand(tree);
                            operand2 = getOperand(tree);
                            tree.push(new ExpressionTree(new ExpressionTreeOp(1, temp, 0), operand2, operand1));
                            stack.push(operator);
                        }//当得到的符号的优先级小于栈顶元素或者优先级相同时时，数字栈出来两个运算数，形成新的树进栈
                    }
                }
                else
                    tree.push(new ExpressionTree(new ExpressionTreeOp(2,' ',Integer.parseInt(tempToken)), null, null));
            }
            while(!stack.empty()){
                String a = String.valueOf(stack.pop());
                operator = a.charAt(0);
                operand1 = getOperand(tree);
                operand2 = getOperand(tree);
                tree.push(new ExpressionTree
                        (new ExpressionTreeOp(1, operator, 0), operand2, operand1));
            }
            return tree.peek();
        }

        public String getTree()
        {
            return (tree.peek()).printTree();
        }
    public int getResult(){
        return tree.peek().evaluateTree();
    }

        public String PostOrder(){
            Iterator iterator =  tree.peek().iteratorPostOrder();
            String result="";
            for (;iterator.hasNext();)
                result +=iterator.next()+" ";
            return result;
        }
    }


